import { getuserlist,usercount } from "@/request/api";
const state ={
    userlist:[],
    pageNum:1,//页码
    pageSize:4,//当前页渲染条数
    count:0//总条数
};
const getters = {
    getUser(state){
        return state.userlist
    },
    getSize(state){
        return state.pageSize
    },
    getCount(state){
        return state.count
    }
};
const mutations = {
    REQ_USERLIST(state,payload){
        state.userlist = payload
    },
    REQ_COUNT(state,payload){
        state.count = payload
    },
    //修改页面事件
    REQ_PAGENUM(state,payload){
        state.pageNum = payload
    }
};
const actions = {
    getUserAction(context){
        console.log(context);
        getuserlist({
            size:context.state.pageSize,
            page:context.state.pageNum,
        }).then(res=>{
            console.log(res);
            let list = res.list ? res.list : []
            if (context.state.pageNum != 1 && list.length == 0) {
                context.dispatch('changePageAction',res.state.pageNum-1)
            }
            context.commit('REQ_USERLIST',res.list)
        })
    },
    //获取总条数
    getCountAction({commit}){
        usercount().then(res=>{
            commit('REQ_COUNT',res.list[0].total)
        })
    },
    //切换列表重新获取列表
    changePageAction(context,payload){
        context.commit('REQ_PAGENUM',payload);
        context.dispatch("getUserAction")
    }
}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}